package com.neuedu.helloworld.ticket;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.neuedu.helloworld.R;
import com.neuedu.helloworld.station.ListLetterView;
import com.neuedu.helloworld.station.Station;
import com.neuedu.helloworld.station.StationUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StationActivity extends AppCompatActivity {

    private ListView lvStationList;
    private ListLetterView listLetterView;
    private BaseAdapter adapter;
    List<Station> data = new ArrayList<>();

    Map<String,Integer> firstLetterIndexMap = new HashMap();

    //居中浮动显示一个字符
    private TextView overlay;

    private String[] sections;
    private Handler handler;
    private OverlayThread overlayThread;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_station);


        lvStationList = findViewById(R.id.lvStationList);
        listLetterView = findViewById(R.id.listLetterView);

        initStation();

        adapter = new MyAdapter(data);
        lvStationList.setAdapter(adapter);
        listLetterView.setListener(new ListLetterView.OnLetterTouchListrener() {
            @Override
            public void onTouchingLetterChanged(String letter) {

                Integer letterFirstIndex = firstLetterIndexMap.get(letter);
                if(letterFirstIndex != null){
                    lvStationList.setSelection(letterFirstIndex);
                }


                overlay.setText(letter);
                overlay.setVisibility(View.VISIBLE);

                //
                handler.removeCallbacks(overlayThread);


                handler.postDelayed(overlayThread, 1500);

            }
        });

        lvStationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Station station = data.get(position);


                Intent intent = getIntent();
                intent.putExtra("stationName", station.getStation_name());
                setResult(200, intent);

                finish();
            }
        });

        handler = new Handler();
        overlayThread = new OverlayThread();

        initOverlay();


    }


    /**
     *   如果 在 /data/data/com.neuedu.helloworld/db/statsion.db  目录中不存在
     *
     *
     *         asserts   把文件copy
     *
     *
     *         /data/data/com.neuedu.helloworld/db
     *
     *
     *
     *
     *   从 /data/data/com.neuedu.helloworld/db/statsion.db 读取, sqlite数据库
     *
     *
     */
    private void initStation() {


        data  = StationUtils.getAllStations(this);


        /**
         * 找到每个首字母第一次出现的位置
         *  热门         北京      0
         *  A            昂昂溪    10
         */
        String lastLetter = null;
        for (int i = 0; i < data.size(); i++) {
            Station station = data.get(i);

            String letter  = station.getSort_order();
            if(!letter.equals(lastLetter)){
                firstLetterIndexMap.put(letter,i ); //第一次出现应该显示字符的位置
                station.setShowLetter(true);
            }
            lastLetter = station.getSort_order();

        }


    }


    class MyAdapter extends BaseAdapter{


        private List<Station> data ;

        public MyAdapter(List data) {
            this.data = data;
        }


        //表示需要渲染多少次数据
        @Override
        public int getCount() {
            return data.size();
        }

        //获取某一行数据
        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        //某一行需要渲染的内容 View
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

//            View view = null;
            //同布局文件渲染 试图对象
            Station station = (Station) getItem(position);

            View view  =  getLayoutInflater().inflate(R.layout.activity_station_list_view_item,null);

            TextView stationLetter = view.findViewById(R.id.stationLetter);
            TextView stationName  = view.findViewById(R.id.stationName);

            stationLetter.setText(station.getSort_order());
            stationName.setText(station.getStation_name());

            if(station.isShowLetter()){
//                stationLetter.setVisibility(View.GONE);  //没有占位
                stationLetter.setVisibility(View.VISIBLE);
//                stationLetter.setVisibility(View.INVISIBLE);
            }


            return view;
        }
    }




    // 初始化汉语拼音首字母弹出提示框(TextView)
    private void initOverlay() {
        LayoutInflater inflater = LayoutInflater.from(this);


        overlay = (TextView) inflater.inflate(R.layout.station_overlay, null);
        overlay.setVisibility(View.INVISIBLE);



        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                PixelFormat.TRANSLUCENT);

        //系统的服务
        WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        windowManager.addView(overlay, lp);

    }


    private class OverlayThread implements Runnable {
        @Override
        public void run() {
            overlay.setVisibility(View.GONE);
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = getIntent();
            intent.putExtra("stationName", "");
            setResult(200, intent);

            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }






}